package com.enterprise.controller.api.message;

import com.alibaba.fastjson.JSON;
import com.enterprise.controller.api.common.BaseResponse;
import com.enterprise.controller.api.message.vo.MessageResp;
import com.enterprise.entity.Messages;
import com.enterprise.entity.page.PageModel;
import com.enterprise.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RequestMapping("/api/message")
@Controller("apiMessageController")
public class MessageController {

    @Autowired
    private MessageService messageService;


    @ResponseBody
    @RequestMapping("/queryMessagePage")
    public BaseResponse<PageModel> queryMessagePage(@RequestBody Messages messages){
        log.info("queryMessagePage messages: {}" , JSON.toJSONString(messages));
        BaseResponse response = new BaseResponse();
        messages.setPageSize(6);
        List<Messages> messages1 = messageService.selectPage(messages);
        List<MessageResp> messageResps = new ArrayList<>();
        for (Messages message:messages1) {
            MessageResp messageResp = new MessageResp();
            BeanUtils.copyProperties(message,messageResp);
            messageResps.add(messageResp);
        }
        log.info("queryMessagePage messageResps: {}" , JSON.toJSONString(messageResps));
        response.setData(messageResps);
        return response;
    }

    @ResponseBody
    @RequestMapping("/queryMessageList")
    public BaseResponse<List<MessageResp>> queryCarList(){
        BaseResponse response = new BaseResponse();
        List<Messages> messageList = messageService.selectList(new Messages());
        List<MessageResp> messageResps = new ArrayList<>();
        for (Messages message:messageList) {
            MessageResp messageResp = new MessageResp();
            BeanUtils.copyProperties(message,messageResp);
            messageResps.add(messageResp);
        }
        log.info("queryMessageList messageResps: {}" , JSON.toJSONString(messageResps));
        response.setData(messageResps);
        return response;
    }


    @ResponseBody
    @RequestMapping("/queryMessageView/{id}")
    public BaseResponse<MessageResp> queryMessageView(@PathVariable("id") String id){
        BaseResponse response = new BaseResponse();
        Messages message = messageService.selectById(Integer.valueOf(id));
        MessageResp messageResp = new MessageResp();
        BeanUtils.copyProperties(message,messageResp);
        log.info("queryMessageView messageResp: {}" , JSON.toJSONString(messageResp));
        response.setData(messageResp);
        return response;
    }

    @ResponseBody
    @RequestMapping("/saveMessage")
    public BaseResponse queryMessageView(@RequestBody Messages message){
        BaseResponse baseResponse =  new BaseResponse();
        messageService.insert(message);
        return baseResponse;
    }


}
